package org.inote.service.impl;

import org.inote.dao.UserMapper;
import org.inote.pojo.tablePojo.User;
import org.inote.service.UserService;
import org.inote.utils.StringFormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {

    UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean register(String phone, String password) {
        if (StringFormUtil.isEmpty(phone, password)){
            return false;
        }
        User user = new User();
        user.setUserPhone(phone);
        user.setUserPassword(password);
        boolean success = true;
        try {
            userMapper.addUser(user);
        } catch(Exception e) {
            System.out.println("手机号已被注册");
            success =  false;
        }
        return true;
    }

    @Override
    public boolean phoneExist(String phone) {
        User user = userMapper.getUserByPhone(phone);
        if (user == null){
            return false;
        }
        return true;
    }

    @Override
    public User getUser(String phone) {
        return userMapper.getUserByPhone(phone);
    }

    @Override
    public boolean updatePasswordById(Integer id, String newPassword) {
        System.out.println(id);
        System.out.println(newPassword);
        int i = userMapper.updatePasswordById(id, newPassword);
        if (i<=0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }
}
